/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.progress.materialprogressbar;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 自定义ProgressBar基类，提供额外特性扩展
 * tint, intrinsic padding, hide background
 *
 * @since 2021-05-08
 */
public class MaterialProgressBar extends Component {
    @Deprecated
    protected boolean mUseIntrinsicPadding = true;
    protected boolean mShowBackground = true;
    protected Color mProgressTint;
    protected Color mSecondaryProgressTint;
    protected Color mProgressBackgroundTint;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public MaterialProgressBar(Context context) {
        super(context);
        initAttrs(context, null);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 自定义属性
     */
    public MaterialProgressBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initAttrs(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 自定义属性
     * @param styleName 主题名称
     */
    public MaterialProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initAttrs(context, attrSet);
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrSet 自定义属性
     * @param resId 资源ID
     */
    public MaterialProgressBar(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        initAttrs(context, attrSet);
    }

    private void initAttrs(Context context, AttrSet attrSet) {
        if (attrSet != null) {
            if (attrSet.getAttr("mpb_showProgressBackground").isPresent()) {
                mShowBackground = attrSet.getAttr("mpb_showProgressBackground").get().getBoolValue();
            } else {
                mShowBackground = true;
            }
            if (attrSet.getAttr("mpb_useIntrinsicPadding").isPresent()) {
                mUseIntrinsicPadding = attrSet.getAttr("mpb_useIntrinsicPadding").get().getBoolValue();
            } else {
                mUseIntrinsicPadding = true;
            }
            if (attrSet.getAttr("mpb_progressTint").isPresent()) {
                mProgressTint = attrSet.getAttr("mpb_progressTint").get().getColorValue();
            } else {
                mProgressTint = null;
            }
            if (attrSet.getAttr("mpb_secondaryProgressTint").isPresent()) {
                mSecondaryProgressTint = attrSet.getAttr("mpb_secondaryProgressTint").get().getColorValue();
            } else {
                mSecondaryProgressTint = null;
            }
            if (attrSet.getAttr("mpb_progressBackgroundTint").isPresent()) {
                mProgressBackgroundTint = attrSet.getAttr("mpb_progressBackgroundTint").get().getColorValue();
            } else {
                mProgressBackgroundTint = null;
            }
        }
    }
}
